/*
 * Copyright (C) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_errno.h"
#include "iot_i2c.h"
#include "bmp280.h"

#define I2C1_SDA_GPIO 0
#define I2C1_SCL_GPIO 1
#define IOT_GPIO_FUNC_GPIO_0_I2C1_SDA  6
#define IOT_GPIO_FUNC_GPIO_1_I2C1_SCL 6
#define WIFI_IOT_I2C_IDX_1 1

BMP280_HandleTypedef bmp280;
float pressure, temperature, asl;

static void I2CTask(void)
{
    uint8_t ret;
    
    //GPIO_0复用为I2C1_SDA
    IoTGpioInit(I2C1_SDA_GPIO);
    IoTGpioSetFunc(I2C1_SDA_GPIO, IOT_GPIO_FUNC_GPIO_0_I2C1_SDA);

    //GPIO_1复用为I2C1_SCL
    IoTGpioInit(I2C1_SCL_GPIO);
    IoTGpioSetFunc(I2C1_SCL_GPIO, IOT_GPIO_FUNC_GPIO_1_I2C1_SCL);

    //baudrate: 400kbps
    IoTI2cInit(WIFI_IOT_I2C_IDX_1, 400000);

    bmp280_init_default_params(&bmp280.params);
    	while (!bmp280_init(&bmp280, &bmp280.params)) {
		printf("BMP280 initialization failed\n");
	}
    osDelay(200);
    while (1)
    {
        //读取温度、气压、海拔数据
        

    }
}

static void I2CExampleEntry(void)
{
    osThreadAttr_t attr;
    attr.name = "I2CTask";
    attr.stack_size = 1024 * 8;
    attr.priority = osPriorityNormal;

    osThreadNew((osThreadFunc_t)I2CTask, NULL, &attr);
}

APP_FEATURE_INIT(I2CExampleEntry);